package com.zzptc.sgz.shop.controller;

import com.zzptc.sgz.shop.dao.CategroryDaoI;
import com.zzptc.sgz.shop.dao.CategroryDaoImpl;
import com.zzptc.sgz.shop.dao.ProductDaoI;
import com.zzptc.sgz.shop.dao.ProductDaoImpl;
import com.zzptc.sgz.shop.vo.Category;
import com.zzptc.sgz.shop.vo.Product;
import com.zzptc.sgz.shop.vo.User;

import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;

@javax.servlet.annotation.WebServlet(name = "ProductServlet",value = "/ProductServlet")
public class ProductServlet extends javax.servlet.http.HttpServlet {
    protected void doPost(javax.servlet.http.HttpServletRequest request,
                          javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
       doGet(request,response);
    }

    protected void doGet(javax.servlet.http.HttpServletRequest request,
                         javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        out.println("==文化====");
        ProductDaoI dao=new ProductDaoImpl();
        CategroryDaoI cdao=new CategroryDaoImpl();
        List<Product> allproduct=null;
        List<Category> categoryList=null;
        List<Product> userfootList=null;
        List<Product> hotrecoment=null;
        HttpSession session = request.getSession();
        try {
            //商品列表
             allproduct = dao.getAllproduct();
             //分类
             categoryList=cdao.getAll();
             //没有登陆的随机热门推荐
            hotrecoment=dao.getHotRecommend("");
            Object obj=session.getAttribute("user");
            if(obj!=null) {
                User u=(User)obj;
                //获取用户历史足迹
                System.out.println("获取用户足迹---");
                userfootList = dao.getAllUserFoot(u.getuId());
                //登陆后根据用户历史足迹热门推荐
                System.out.println("获取热门推荐---");
                hotrecoment=dao.getHotRecommend(u.getuId());
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        if(hotrecoment.size()<2){
            //没有热门推荐
            System.out.println("热门推荐数量不够2条");
            hotrecoment=allproduct;
        }
        session.setAttribute("list",allproduct);
        session.setAttribute("categoryList",categoryList);
        session.setAttribute("userfootList",userfootList);
        session.setAttribute("hotrecoment",hotrecoment);
        request.getRequestDispatcher("index111.jsp").forward(request,response);

    }
}
